from django import template
from thoughts.models import Witness

def thought(context, t):
    if t.article_notes:  
        notes_count = t.article_notes.count()
    if t.book_notes:
        notes_count = notes_count + t.book_notes.count()
    to_return ={
    'thought': t,
    'notes_count': notes_count,   
    }
    if context['user'].is_authenticated():
        try:
             Witness.objects.get(thought = t, user = context['user'])
             witnessing = True
        except Witness.DoesNotExist:
             witnessing = False
        to_return.update({
            'witness' : witnessing,
            'authenticated': True,
        })
    else:
       to_return.update({
           'authenticated': False,
       })
    return to_return 

def shared_thought(context, t):
    if t.article_notes:  
        notes_count = t.article_notes.count()
    if t.book_notes:
        notes_count = notes_count + t.book_notes.count()
    to_return ={
    'thought': t,
    'notes_count': notes_count,   
    }
    if context['user'].is_authenticated():
        try:
             Witness.objects.get(thought = t, user = context['user'])
             witnessing = True
        except Witness.DoesNotExist:
             witnessing = False
        to_return.update({
            'witness' : witnessing,
            'authenticated': True,
        })
    else:
       to_return.update({
           'authenticated': False,
       })
    return to_return 

register = template.Library()

register.inclusion_tag('board.html', takes_context = True)(shared_thought)
register.inclusion_tag('thoughts/thought.html', takes_context = True)(thought)
